<template>
  <div class="login_container">
    <div class="login_box">
      <div class="vartar">
        <img class="img" src="../../assets/logo.png" />
      </div>
      <el-form
        :model="login_from"
        :rules="login_rules"
        label-width="0px"
        class="login_form"
        ref="loginRef"
      >
        <el-form-item prop='username'>
          <el-input
            v-model="login_from.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop='password'>
          <el-input
            v-model="login_from.password"
            placeholder="请输入用户密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginOk">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  data () {
    return {
      login_from: {
        username: 'admin',
        password: '123456'
      },
      login_rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  mounted () {},
  methods: {
    resetLogin () {
      this.$refs.loginRef.resetFields()
    },
    loginOk () {
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) return

        var { data } = await login(this.login_from)
        if (data.meta.status === 200) {
          this.$store.commit('setUser', data.data.token)
          this.$message.success('登录成功')
          this.$router.push('/home')
        } else {
          return this.$message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    position: absolute;
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 3px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .vartar {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 100px;
      height: 100px;
      border: 1px solid pink;
      border-radius: 50%;
      box-sizing: border-box;
      padding: 10px;
      background-color: #fff;
      .img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid pink;
        background-color: #eee;
      }
    }
  }
  .login_form {
    box-sizing: border-box;
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 20px;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
